package com.bawei.week.park

import android.graphics.pdf.PdfDocument.Page
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import androidx.paging.Pager
import androidx.paging.PagingConfig
import androidx.paging.PagingData
import androidx.paging.cachedIn
import com.bawei.lib_base.entity.ParkBean
import com.bawei.lib_base.entity.ParkBeanItem
import com.bawei.lib_base.mvvm.BaseViewModel
import com.bawei.lib_base.utils.ApiService
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.Flow
import javax.inject.Inject

@HiltViewModel
class ParkViewModel @Inject constructor(val apiService: ApiService) : BaseViewModel(){
    val parkSuc = MutableLiveData<ParkBean>()

    val parkAddSuc = MutableLiveData<ParkBeanItem>()

    val parkModifySuc  = MutableLiveData<ParkBeanItem>()

    fun getParkByType(para : Any,type : String) : Flow<PagingData<ParkBeanItem>> {
        return Pager(config = PagingConfig(pageSize = 10), pagingSourceFactory = {
            ParkDataSource(apiService,para as String,type)
        }).flow.cachedIn(viewModelScope)
    }

    fun getParkAdd(parkBeanItem: ParkBeanItem){
        httpRetrofit({apiService.getParkAdd(mapToBody(parkBeanItem))},parkAddSuc :: postValue)
    }

    fun updatePark(id:Int,beanItem: ParkBeanItem) {
        httpRetrofit({ apiService.updatePark(id,mapToBody(beanItem)) }, parkModifySuc::postValue)
    }

    fun getParkDelete(id : Int){
        httpRetrofit({apiService.getParkDelete(id)},parkSuc :: postValue)
    }
}